package com.xxyf.service;

import com.xxyf.mapper.UsernameMapper;
import com.xxyf.pojo.Username;
import com.xxyf.tools.Code;
import com.xxyf.tools.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author 小小怡飞
 * @Date 2022/8/24 21:09
 * @Version JDK 8
 */
@Service
public class UsernameServiceImp implements UsernameService{
    @Autowired
    UsernameMapper usernameMapper;

    @Override
    public Rest login(String username, String password) {
            if (StringUtils.hasText(username) && StringUtils.hasText(password)){
                Username loging = usernameMapper.loging(username, password);
                if (loging!=null){
                    return new Rest(loging,"登录成功，欢迎使用!");
                }

            }
            return new Rest(Code.erro,"登录失败，用户名或密码错误!");
    }

    @Override
    public Rest reg(String id, String pass,String old) {
        if (StringUtils.hasText(id) && StringUtils.hasText(pass) && StringUtils.hasText(old)){
            if (pass.equals(old)){
                int reg = usernameMapper.reg(id, pass);
                if (reg>0){
                    return new Rest(Code.succeed,"注册成功，账号为:"+id+",密码为:"+pass+"请牢记");
                }else {
                    return new Rest(Code.erro,"新建用户失败,账号密码输入错误");
                }
            }else {
                return new Rest(Code.erro,"新建用户失败,两次密码输入不一致");
            }
        }
        return new Rest(Code.erro,"请求错误");
    }

    @Override
    public Rest check(String id) {
        if (StringUtils.hasText(id)){
            String check = usernameMapper.check(id);
            if (!id.equals(check)){
                return new Rest(Code.succeed);
            }else {
                return new Rest(Code.erro,"账号已存在");
            }
        }
        return new Rest(Code.erro,"请求错误");
    }
}
